(function($){
    $.fn.imagezoom = function(options){
        var settings = {
            /* xzoom: 180,
             yzoom: 180*/
        };
        if(options) {
            $.extend(settings, options);
        }

        var self = this;
        $(this).bind("mouseenter", function(ev){
            var imageLeft = $(this).offset().left;
            var imageTop = $(this).offset().top;
            var imageWidth = $(this).get(0).offsetWidth;
            var imageHeight = $(this).get(0).offsetHeight;
            var boxTop = $(this).parent().offset().top;
            leftpos = imageLeft - settings.xzoom - settings.offset;
            if(leftpos < 0){
                leftpos = imageLeft + imageWidth  + settings.offset;
            }

            $("div.zoomDiv").css({ top: boxTop,left: leftpos });
            $("div.zoomDiv").width(settings.xzoom);
            $("div.zoomDiv").height(settings.yzoom);
            $("div.zoomDiv").show();
            $("div.zoomMask").show();

            $(this).css('cursor','crosshair');

            $(document.body).mousemove(function(e){

                mouse = new MouseEvent(e);
                if(mouse.x<imageLeft || mouse.x>imageLeft+imageWidth || mouse.y<imageTop || mouse.y>imageTop+imageHeight){
                    mouseOutImage();
                    return;
                }

                var bigwidth = $(".bigimg").get(0).offsetWidth;
                var bigheight = $(".bigimg").get(0).offsetHeight;

                var scaley ='x';
                var scalex ='y';

                if(isNaN(scalex)|isNaN(scaley)){
                    var scalex = (bigwidth/imageWidth);
                    var scaley = (bigheight/imageHeight);
                    $("div.zoomMask").width((settings.xzoom)/scalex );
                    $("div.zoomMask").height((settings.yzoom)/scaley);
                    $("div.zoomMask").css('visibility','visible');
                }



                xpos = mouse.x- $("div.zoomMask").width()/2 ;
                ypos = mouse.y- $("div.zoomMask").height()/2 ;

                xposs = mouse.x- $("div.zoomMask").width()/2 - imageLeft;
                yposs = mouse.y- $("div.zoomMask").height()/2 - imageTop ;

                $("div.zoomMask").css({ top:ypos,left:xpos});
                $("div.zoomDiv").get(0).scrollLeft = xposs * scalex;
                $("div.zoomDiv").get(0).scrollTop  = yposs * scaley;

            });
        });

        function mouseOutImage(){
            //$(self).attr("alt",noalt);
            $(document.body).unbind("mousemove");
            $("div.zoomMask").hide();
            $("div.zoomDiv").hide();
        }
    }

})(jQuery);


function MouseEvent(e) {
    this.x = e.pageX;
    this.y = e.pageY;
}